package com.ymk.cmd;

import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.bo.orders.OrdersBO;
import com.ymk.core.code.cmd.OrdersCmdCode;
import com.ymk.core.code.req.ReqCode;
import com.ymk.service.IOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class OrdersPatchCmd extends ACmd {

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        this.ordersService.update(reqData);

        return respSign;
    }

    @Override
    public CmdSign executeServer(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        OrdersBO reqData = super.getData(sign, OrdersBO.class);

        if (ReqCode.ORDERS_PATCH_PAYMENT_ID.equals(sign.getReqCode())) {
            OrdersBO ordersBO = new OrdersBO();
            ordersBO.setId(reqData.getId());
            ordersBO.setPaymentId(reqData.getPaymentId());
            super.setCreatedData(sign, ordersBO);
            this.ordersService.update(ordersBO);

        } else if (ReqCode.PAY_SYNC_ORDERS.equals(sign.getReqCode())) {
            OrdersBO ordersBO = new OrdersBO();
            ordersBO.setId(reqData.getId());
            ordersBO.setPaymentId(reqData.getPaymentId());
            ordersBO.setPayType(reqData.getPayType());
            ordersBO.setSuperiorAmount(reqData.getSuperiorAmount());
            ordersBO.setProfitAmount(reqData.getProfitAmount());
            ordersBO.setStatus(reqData.getStatus());
            setModifiedData(sign, ordersBO);
            this.ordersService.update(ordersBO);

        } else {

            this.ordersService.update(reqData);
        }
        return respSign;
    }

    @Override
    public String getCmdCode() {
        return OrdersCmdCode.ORDERS_PATCH;
    }

    @Autowired
    private IOrdersService ordersService;
}